package com.dlsy.ygj.dao.vindicateData.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import net.taobaoge.common.framework.dao.impl.BaseDAOImpl;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.util.CollectionUtils;

import com.dlsy.ygj.dao.vindicateData.IFavouredRangeInfoDAO;
import com.dlsy.ygj.entity.FavouredRangeInfo;
import com.dlsy.ygj.entity.MainItemInfo;

public class FavouredRangeInfoDAO extends BaseDAOImpl<FavouredRangeInfo>
		implements IFavouredRangeInfoDAO {

	@SuppressWarnings("unchecked")
	public List<FavouredRangeInfo> findFavouredRangeInfoByProperty(
			String value, Integer stroreId) {
		Criteria criteria = getSession()
				.createCriteria(FavouredRangeInfo.class);
		criteria.add(Restrictions.and(Restrictions.or(Restrictions.like(
				"favouredRangeNo", "%" + value + "%"), Restrictions.like(
				"favouredRangeName", "%" + value + "%")), Restrictions.eq(
				"storefrontId", stroreId)));
		List<FavouredRangeInfo> favouredRangeInfos = criteria.list();
		if (favouredRangeInfos != null && favouredRangeInfos.size() > 0) {
			return favouredRangeInfos;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredRangeInfo> checkExist(final Integer favouredRangeId,
			final String value, final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredRangeInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredRangeInfo.class);
				criteria.add(Restrictions.and(Restrictions.and(Restrictions.or(
						Restrictions.eq("favouredRangeNo", value), Restrictions
								.eq("favouredRangeName", value)), Restrictions
						.not(Restrictions
								.eq("favouredRangeId", favouredRangeId))),
						Restrictions.eq("storefrontId", stroreId)));
				List<FavouredRangeInfo> favouredRangeInfos = criteria.list();
				if (favouredRangeInfos != null && favouredRangeInfos.size() > 0) {
					return favouredRangeInfos;
				}
				return null;
			}
		});
	}

	public List<FavouredRangeInfo> findAll(Integer stroreId) {
		try {
			List<FavouredRangeInfo> favouredRangeInfos = findByProperty(
					FavouredRangeInfo.class, "storefrontId", stroreId);
			if (!CollectionUtils.isEmpty(favouredRangeInfos)) {
				return favouredRangeInfos;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<FavouredRangeInfo> checkExist(final String value,
			final Integer stroreId) {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			public List<FavouredRangeInfo> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = getSession().createCriteria(
						FavouredRangeInfo.class);
				criteria.add(Restrictions.and(Restrictions.or(Restrictions.eq(
						"favouredRangeNo", value), Restrictions.eq(
						"favouredRangeName", value)), Restrictions.eq(
						"storefrontId", stroreId)));
				List<FavouredRangeInfo> favouredRangeInfos = criteria.list();
				if (favouredRangeInfos != null && favouredRangeInfos.size() > 0) {
					return favouredRangeInfos;
				}
				return null;
			}
		});
	}

	@SuppressWarnings("unchecked")
	public List<FavouredRangeInfo> findRangeById(final Collection<Integer> IDs) {
		 return getHibernateTemplate().executeFind(new HibernateCallback() {
				
				public List<FavouredRangeInfo> doInHibernate(Session session) throws HibernateException,
						SQLException {
					Query query=session.createQuery("From FavouredRangeInfo f Where f.favouredRangeId in (:condtion)");
					query.setParameterList("condtion", IDs);
					List<FavouredRangeInfo> ranges=query.list();
					if(ranges !=null && ranges.size()>0){
						return ranges;
					}
					return null;
				}
			});
	}
}
